package mapViewer;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import context.*;

import simpleMapService.Location;

// ******************************************************************
// *** INNER CLASS: MapSheet
// ******************************************************************
public class MapSheet implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
	
  private String title;
  private MapLoader mapLoader;
  private Location mapCenter;
  private int scale;
  
  private static String selectedMapSheet="";
  private static Hashtable mapSheetSet=new Hashtable();
  public static Hashtable getMapSheetSet() { return(mapSheetSet); }
  public static String getSelectedMapSheet() { return(selectedMapSheet); }
  
  public static void setSelectedMapSheet(String title)
  { if(DEBUG) Debug.println("MapSheet.setSelectedMapSheet: "+title);
	if(mapSheetSet.containsKey(title)) selectedMapSheet=title;
	else selectedMapSheet=(String)mapSheetSet.keys().nextElement();
  }
  
  public static MapSheet add(String title,Location mapCenter,int scale)
  { if(mapSheetSet.containsKey(title)) return(null);
    return(new MapSheet(title,mapCenter,scale));
  }
  
  public static MapSheet get(String title)
  { return((MapSheet)mapSheetSet.get(title)); }
  
  public static MapSheet remove(String title)
  { return((MapSheet)mapSheetSet.remove(title)); }



  // ****************************************************************
  // *** CONSTRUCTOR: MapSheet
  // ****************************************************************
  private MapSheet(String title,Location mapCenter,int scale)
  { this.title=title;
    this.mapCenter=mapCenter;
    this.scale=scale;
    mapSheetSet.put(title,this);
  }
  
  public String getTitle() { return(title); }
  public Location getMapCenter() { return(mapCenter); }
  public int getScale() { return(scale); }
  
  public MapLoader getMapLoader()
  { if(mapLoader==null) mapLoader=new MapLoader(mapCenter,scale);
    return(mapLoader);
  }
  

  
  public void writeRecord(RecordStore rs,int recordId) throws IOException,RecordStoreException
  { ByteArrayOutputStream bout=new ByteArrayOutputStream();
    DataOutputStream dout=new DataOutputStream(bout);
    if(DEBUG) Debug.println("MapSheet.WRITE["+recordId+"]: "+this);
    dout.writeUTF(title);
    dout.writeDouble(mapCenter.getLongitude());
    dout.writeDouble(mapCenter.getLatitude());
    dout.writeInt(scale);
    byte[] record=bout.toByteArray();
    int n=rs.addRecord(record, 0, record.length);
    if(DEBUG) Debug.println("MapSheet.WRITTEN["+n+"]: "+this);
    dout.close();
    bout.close();
  }
  

  public static void readRecord(RecordStore rs,int recordId) throws IOException,RecordStoreException
  { int n=rs.getRecordSize(recordId);
    byte[] rsData=new byte[n];
    ByteArrayInputStream bin = new ByteArrayInputStream(rsData);
    DataInputStream din = new DataInputStream(bin);
    rs.getRecord(recordId,rsData,0);
    String title=din.readUTF();
    double lon=din.readDouble();
    double lat=din.readDouble();
    Location mapCenter=new Location(lon,lat);
    int scale=din.readInt();
    bin.close();
    din.close();
    MapSheet mapSheet=add(title,mapCenter,scale);
    if(DEBUG) Debug.println("MapSheet.READ["+recordId+"]: "+mapSheet);
  }
  

  public String toString()
  { if(DEBUG) return("MapSheet[title="+title
		  +",mapCenter="+mapCenter
		  +",scale="+scale
		  +']');
    else return(super.toString());
  }
  
}
